package com.cailiangchen.JDBC;

import com.cailiangchen.domain.User;

import java.sql.*;

public class JDBCUtil {
    public static ResultSet rs;
    public static Statement stmt;
    public static Connection conn;

    public JDBCUtil() throws ClassNotFoundException, SQLException {
        String url = "jdbc:mysql://106.54.73.19:3306/chat_info?characterEncoding=utf-8";
        String user = "outside";
        String pwd = "123456";
        Class.forName("com.mysql.cj.jdbc.Driver");
        conn = DriverManager.getConnection(url,user,pwd);
        stmt = conn.createStatement();
        rs = stmt.executeQuery("select * from user_info;");
    }

    /**
     * 插入注册数据
     * @param user 用户信息
     * @throws SQLException
     */
    public void insertMessage(User user) throws SQLException {
        String userName = user.getUser();
        String pwd = user.getPwd();
        stmt.executeUpdate("insert into user_info values ('"+userName+"','"+pwd+"')");
    }

    public boolean check(User user) throws SQLException {
        while(rs.next()){
            if(rs.getString("user").equals(user.getUser())&&rs.getString("pwd").equals(user.getPwd())){
                return true;
            }
        }
        return false;
    }

    public void close() throws SQLException {
        rs.close();
        stmt.close();
        conn.close();
    }
    public static void main(String[] args) throws SQLException, ClassNotFoundException {
        new JDBCUtil();
    }
}
